clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Table5.log", replace
display "$S_TIME  $S_DATE"

set more off
set varabbrev off, perm

timer on 1

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 

use `finalfile_yearly'
xtset instkey year

generate avg_logassets_sqY = avg_logassetsY*avg_logassetsY
regress avg_logassets_sqY avg_logassetsY
predict sizesq_orth, residuals
label variable avg_logassetsY "Size (log assets)"
label variable sizesq_orth "Size (log assets) squared, orthogonalized"

generate avg_tassetsW_BN = avg_totassetsWY/(10^9)
generate avg_tassetsW_BN_sq = avg_tassetsW_BN * avg_tassetsW_BN

local rhs L.avg_logassetsY L.sizesq_orth  ///
L.avg_mtbWY L.avg_roaaWY L.lev_fps L.reteY growth2WY L.avg_tieroneratioWY

generate year_2007 = (year == 2007)
generate year_2008 = (year == 2008)

drop if year >= 2010

foreach x of varlist divgrowthY1 tpogrowthY1  {
	
		quietly regress F.returnY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008  `rhs' , cluster(instkey)
		gen inreg = e(sample) 
		quietly regress F.returnY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008   if inreg == 1 , cluster(instkey) 
		outreg2 using Table5_`x', ///
		excel stats(coef tstat) par(tstat) dec(2) adjr2 ///
		append
		drop inreg

		quietly regress F.returnY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008 `rhs', cluster(instkey)
		outreg2 using Table5_`x', ///
		excel stats(coef tstat) par(tstat) dec(2) adjr2 ///
		append
		
		quietly regress F.avg_roaaWY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008 `rhs' if F.returnY != ., cluster(instkey)
		gen inreg = e(sample) 
		quietly regress F.avg_roaaWY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008  if inreg == 1 , cluster(instkey) 
		outreg2 using Table5_`x', ///
		excel stats(coef tstat) par(tstat) dec(2) adjr2 ///
		append
		drop inreg

		quietly regress F.avg_roaaWY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008  `rhs' if F.returnY != ., cluster(instkey)
		outreg2 using Table5_`x', ///
		excel stats(coef tstat) par(tstat) dec(2) adjr2 ///
		append
		
	}

display "$S_TIME  $S_DATE"
log close
clear
exit
